#pragma once

#include "common.hpp"



class Init
{
public:
    Init()
    {
        //umask(0);
        int n = ::mkfifo(pipefile.c_str(), gmode);
        if (n < 0)
        {
            std::cerr << "mkfile error" << std::endl;
        }
        else
        {
            std::cout << "mkfile success" << std::endl;
        }
        sleep(2);
    }

    ~Init()
    {
        int n = ::unlink(pipefile.c_str());
        if (n < 0)
        {
            std::cerr << "unlink error" << std::endl;
        }
        std::cout << "unlink success" << std::endl;
    }
};

Init init;

class server
{
public:
    server() : _fd(gdefaultfd)
    {
        // cout<<"server init"<<endl;
    }
    ~server()
    {
        // cout<<"server delete"<<endl;
    }
    int OpenPipeRead()
    {
        _fd = ::open(pipefile.c_str(), O_RDONLY);
        if (_fd < 0)
        {
            std::cerr << "fd error" << endl;
        }
        return _fd;
    }
    int Recvpipe(std::string *out) // 输出型参数
    {
        char buff[gsize];
        int n = ::read(_fd, buff, sizeof(buff) - 1);
        if (n > 0)
        {
            buff[n] = 0;
            *out = buff;
        }
        return n; // 读取到了信息，返回值>0
    }
    void Closepipe()
    {
        // Closepipehelp(_fd);
        if (_fd > 0)
        {
            ::close(_fd);
        }
    }

private:
    int _fd;
};
